#include "StdAfx.h"
#include "windows.h"
#include "LoopCarrier.h"

LoopCarrier::LoopCarrier(void)
{
}

LoopCarrier::~LoopCarrier(void)
{
}

static volatile bool busy = false;

void LoopCarrier::SetSample(short* s, unsigned int length)
{
	while(busy)
	{
		Sleep(1);
	}
	busy = true;
	sample.clear();
	for(unsigned int i=0; i<length; i++)
	{
		sample.push_back(s[i]);
	}
	curIdx = 0;
	busy = false;
}

/*
static unsigned int modulatorTotal = 0;
static unsigned int startK = 23038;//17408;
static unsigned int endK = 23552;
*/

short* LoopCarrier::CreateCarrierSample(Settings* settings, short* modulatorSample, unsigned int length, std::vector<SampleMarker>* markers)
{
	while(busy)
	{
		Sleep(1);
	}
	busy = true;
	lastReturned.clear();
	for(unsigned int i=0; i<length; i++)
	{
		if(curIdx>=sample.size())
		{
			curIdx = 0;
		}
		short nextVal = sample[curIdx++];
		lastReturned.push_back(nextVal);
	}

	ApplyStopConsonants(modulatorSample, &lastReturned[0], length, markers);
	busy = false;
	return &lastReturned[0];
}